package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.ArithUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNItemInfo;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.OutStorageDetailSNListener;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class OutStorageDetailSNListAdapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater inflater;

    private OutStorageDetailSNListener mOutStorageDetailSNListener;

    private List<SNItemInfo> mSNItemList;

    private List<SNBean.SNInfoBean> mSNList;

    private String mType, mBatchNumber, mActualSendNumber;

    private EditText snEditText;

    private ItemListBean mMaterielDetail;

    private TextView outStorageNumberHead;

    public String getmActualSendNumber() {
        mActualSendNumber = calculatedTotal();
        return mActualSendNumber;
    }

    public OutStorageDetailSNListAdapter(Context context, ItemListBean materielDetail, List<SNBean.SNInfoBean> snList, String batchNumber, String type, OutStorageDetailSNListener outStorageDetailBatchListener) {
        mContext = context;
        mMaterielDetail = materielDetail;
        mSNList = snList;
        mSNItemList = OutStorageUtils.changeSNFormat(snList);
        mBatchNumber = batchNumber;
        mType = type;
        mOutStorageDetailSNListener = outStorageDetailBatchListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mSNItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mSNItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            //领料出库
            TextView materielCode, materielDescription, demandNumber, inventoryAddress, sendedNumber;

            //销售出库
            TextView stockNumberTextView, stockNumber;

            ImageView search;

            switch (mType) {
                case OutStorageUtils.OUT_STORAGE_TYPE_PICKING:
                    convertView = inflater.inflate(R.layout.view_out_storage_detail_first_item, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    demandNumber = (TextView) convertView.findViewById(R.id.demandNumber);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    sendedNumber = (TextView) convertView.findViewById(R.id.sendedNumber);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);
                    snEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    search = (ImageView) convertView.findViewById(R.id.search);
                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    demandNumber.setText(mMaterielDetail.getOrder_qty() + mMaterielDetail.getUnit_name());
                    sendedNumber.setText(mMaterielDetail.getSend_qty() + mMaterielDetail.getUnit_name());
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());
                    inventoryAddress.setText(mMaterielDetail.getLocation_code() + " - " + mMaterielDetail.getLocation_name());
                    snEditText.setText(mBatchNumber);
                    snEditText.setHint("请输入序列号或打包码");
                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(snEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入序列号或打包码");
                            } else {
                                mOutStorageDetailSNListener.onGetSN(snEditText.getText().toString());
                            }
                        }
                    });
                    break;

                case OutStorageUtils.OUT_STORAGE_TYPE_SALE:
                    convertView = inflater.inflate(R.layout.view_out_storage_detail_first_item, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    demandNumber = (TextView) convertView.findViewById(R.id.demandNumber);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    stockNumberTextView = (TextView) convertView.findViewById(R.id.sendedNumberTextView);
                    stockNumber = (TextView) convertView.findViewById(R.id.sendedNumber);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);
                    snEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    search = (ImageView) convertView.findViewById(R.id.search);
                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    demandNumber.setText(mMaterielDetail.getOrder_qty() + mMaterielDetail.getUnit_name());
                    stockNumberTextView.setText("库存数量");
                    stockNumber.setText(mMaterielDetail.getStock_qty() + mMaterielDetail.getUnit_name());
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());
                    inventoryAddress.setText(mMaterielDetail.getLocation_code() + " - " + mMaterielDetail.getLocation_name());
                    snEditText.setText(mBatchNumber);
                    snEditText.setHint("请输入序列号或打包码");
                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(snEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入序列号或打包码");
                            } else {
                                mOutStorageDetailSNListener.onGetSN(snEditText.getText().toString());
                            }
                        }
                    });
                    break;

                case OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION:

                    break;
                case OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION:
                    convertView = inflater.inflate(R.layout.cy_view_out_storage_allocation_listview_first, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);

                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    inventoryAddress.setText(String.valueOf(mMaterielDetail.getLocation_name()));
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());

                    snEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    snEditText.setHint("请输入序列号或打包码");
                    search = (ImageView) convertView.findViewById(R.id.search);

                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(snEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入序列号或打包码");
                            } else {
                                mOutStorageDetailSNListener.onGetSN(snEditText.getText().toString());
                            }
                        }
                    });
                    break;

            }
        } else {
            SNItemInfo snItem = mSNItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_detail_sn, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, snItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.type = (TextView) view.findViewById(R.id.type);
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.icon = (ImageView) view.findViewById(R.id.icon);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, SNItemInfo snItemInfo) {
        viewHolder.code.setText(snItemInfo.getCode());
        switch (snItemInfo.getType()) {
            case OutStorageUtils.SNTYPE_POSITION:
                viewHolder.icon.setImageResource(R.drawable.ico_position);
                viewHolder.type.setText("仓位");
                viewHolder.code.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                break;
            case OutStorageUtils.SNTYPE_PACKAGE:
                viewHolder.icon.setImageResource(R.drawable.ico_package_code);
                viewHolder.type.setText("    打包码");
                viewHolder.code.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
                view.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mOutStorageDetailSNListener.onLookPackageDetail(snItemInfo.getCode());
                    }
                });
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        DialogUtils.getConfirmDialog(mContext, "确定要删除打包码：" + snItemInfo.getCode(), new DialogUtils.ConfirmDialogListener() {
                            @Override
                            public void onConfirm(Dialog dialog) {
                                dialog.cancel();
                                mOutStorageDetailSNListener.onDeleteItem(snItemInfo.getCode(), OutStorageUtils.SNTYPE_PACKAGE);
                            }
                        }).show();
                        return true;
                    }
                });
                break;
            case OutStorageUtils.SNTYPE_SN:
                viewHolder.icon.setImageResource(R.drawable.ico_sn_code);
                viewHolder.type.setText("    序列号");
                viewHolder.code.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                break;
        }
    }

    private class ViewHolder {
        private TextView type, code;

        private ImageView icon;

    }

    //计算实发数量
    private String calculatedTotal() {
        String result = "0";
        if (mSNList == null || mSNList.isEmpty()) {
            return "";
        } else {
            for (int i = 0; i < mSNList.size(); i++) {
                SNBean.SNInfoBean snItem = mSNList.get(i);
                result = ArithUtils.add(result, String.valueOf(snItem.getSn_list().size())).toString();
            }
        }
        return result;
    }

}
